﻿using System;
using System.Data;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using ET;
using BLL;

namespace ERP.PageUC
{
    public partial class ContactUC : UserControl
    {
        private readonly Contact_BLL clsContact = new Contact_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrop();
                GetContact();
            }
        }
        public void GetContact()
        {
            DataTable dtContact = clsContact.BindGirdContact(string.Empty);
            if (dtContact != null)
            {
                ListViewContact.DataSource = dtContact;
                ListViewContact.DataBind();
                //if (txtSearch.Text != "" && dtContact.Rows.Count > 0)
                //{
                //    StrAlert = Alert.AlertSucess(dtContact.Rows.Count.ToString(CultureInfo.InvariantCulture));
                //}
            }
            else
            {
                //StrAlert = Alert.AlertError("Bảng này không tồn tại");
            }
        }
        private void BindDrop()
        {
            var clsDanhXung = new Vocative_BLL();
            var dtDanhXung = clsDanhXung.BindGirdtb_Vocative(string.Empty);
            if (dtDanhXung != null && dtDanhXung.Rows.Count > 0)
            {
                drDanhXung.Items.Clear();
                drDanhXung.Items.Add(new ListItem("-Vui lòng chọn danh xưng-", "-1"));
                drDanhXung.DataSource = dtDanhXung;
                drDanhXung.DataValueField = "Vocative_Id";
                drDanhXung.DataTextField = "Vocative_Name";
                drDanhXung.DataBind();
            }

            var clsChucVu = new Job_BLL();
            var dtChucVu = clsChucVu.BindGirdtb_Job(string.Empty);
            if (dtChucVu != null && dtChucVu.Rows.Count > 0)
            {
                drChucVu.Items.Clear();
                drChucVu.Items.Add(new ListItem("-Vui lòng chọn chức vụ-", "-1"));
                drChucVu.DataSource = dtChucVu;
                drChucVu.DataValueField = "Job_Id";
                drChucVu.DataTextField = "Job_Name";
                drChucVu.DataBind();
            }

            var clsPhongBan = new Department_BLL();
            var dtPhongBan = clsPhongBan.BindGirdtb_Department(string.Empty);
            if (dtPhongBan != null && dtPhongBan.Rows.Count > 0)
            {
                drPhongBan.Items.Clear();
                drPhongBan.Items.Add(new ListItem("-Vui lòng chọn phòng ban-", "-1"));
                drPhongBan.DataSource = dtPhongBan;
                drPhongBan.DataValueField = "Department_Id";
                drPhongBan.DataTextField = "Department_Name";
                drPhongBan.DataBind();
            }

            var clsTinh = new Province_BLL();
            var dtTinh = clsTinh.BindGirdProvince(string.Empty);
            if (dtTinh != null && dtTinh.Rows.Count > 0)
            {
                drTinh.Items.Clear();
                drTinh.Items.Add(new ListItem("-Vui lòng chọn tỉnh-", "-1"));
                drTinh.DataSource = dtTinh;
                drTinh.DataValueField = "Province_Id";
                drTinh.DataTextField = "Province_Name";
                drTinh.DataBind();
            }

            var clsQuanHuyen = new Districts_BLL();
            var dtQuanHuyen = clsQuanHuyen.BindGirdDistricts(string.Empty);
            if (dtQuanHuyen != null && dtQuanHuyen.Rows.Count > 0)
            {
                drQuanHuyen.Items.Clear();
                drQuanHuyen.Items.Add(new ListItem("-Vui lòng chọn tỉnh-", "-1"));
                drQuanHuyen.DataSource = dtQuanHuyen;
                drQuanHuyen.DataValueField = "Districts_Id";
                drQuanHuyen.DataTextField = "Districts_Name";
                drQuanHuyen.DataBind();
            }
        }

        protected void btnSavePopup_Click(object sender, EventArgs e)
        {
            if (lbID.Value == "")
            {
                InsertContact();
            }
            else
            {
                UpdateContact();
            }
        }

        protected void btnReloadPopup_Click(object sender, EventArgs e)
        {
            BindDrop();
            GetContact();
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            var clsContact = new Contact_BLL();
            var dtContact = clsContact.BindGirdContact(string.Empty);
            if (dtContact != null && dtContact.Rows.Count > 0)
            {
                foreach (DataRow dr in dtContact.Rows)
                {
                    if (txtMaDoiTuong.Text == dr["Contact_Alias"].ToString())
                    {
                        kiemtra = true;
                        if (Page.Master != null)
                            ((Label)Page.Master.FindControl("lbThongBao")).Text = "Trùng mã đối tượng !";
                        OpenDialog("#thongbao");
                        if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertContact()
        {
            try
            {
                if (!CheckInsert())
                {
                    var obj = new Contact_ET
                    {
                        Contact_Alias = txtMaDoiTuong.Text,
                        Contact_Name = txtHoVaTen.Text,
                        Contact_BirthDay = txtNgaySinh.Text == string.Empty ? DateTime.Now : DateTime.Parse(txtNgaySinh.Text),
                        Contact_Gender = drGioiTinh.Text,
                        Job_ID = int.Parse(drChucVu.SelectedValue),
                        Contact_Vocative = drDanhXung.Text,
                        Department_ID = int.Parse(drPhongBan.SelectedValue),
                        Contact_Phone = txtDienThoai.Text,
                        Contact_MobilePhone = txtDiDong.Text,
                        Contact_Email = txtEmail.Text,
                        Contact_Address = txtDiaChi.Text,
                        District_ID = int.Parse(drQuanHuyen.SelectedValue),
                        Contact_Date_Add = DateTime.Now,
                        Contact_Date_Modify = DateTime.Now
                    };
                    Contact_BLL.InsertContact(obj);
                    ClearTextBox();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Thêm mới thành công !";
                }
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaDoiTuong.Enabled = true;
                drQuanHuyen.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }
        protected void UpdateContact()
        {
            try
            {
                var obj = new Contact_ET
                {
                    Contact_ID = int.Parse(lbID.Value),
                    Contact_Alias = txtMaDoiTuong.Text,
                    Contact_Name = txtHoVaTen.Text,
                    Contact_BirthDay = DateTime.Parse(txtNgaySinh.Text),
                    Contact_Gender = drGioiTinh.Text,
                    Job_ID = int.Parse(drChucVu.SelectedValue),
                    Contact_Vocative = drDanhXung.Text,
                    Department_ID = int.Parse(drPhongBan.SelectedValue),
                    Contact_Phone = txtDienThoai.Text,
                    Contact_MobilePhone = txtDiDong.Text,
                    Contact_Email = txtEmail.Text,
                    Contact_Address = txtDiaChi.Text,
                    District_ID = int.Parse(drQuanHuyen.SelectedValue),
                    Contact_Date_Add = DateTime.Now,
                    Contact_Date_Modify = DateTime.Now
                };
                Contact_BLL.UpdateContact(obj);
                ((Page.Contact)Page).GetContact();
                if (Page.Master != null)
                    ((Label)Page.Master.FindControl("lbThongBao")).Text = "Sữa thành công !";
            }
            catch (Exception ex)
            {
                if (Page.Master != null) ((Label)Page.Master.FindControl("lbThongBao")).Text = ex.ToString();
            }
            finally
            {
                txtMaDoiTuong.Enabled = false;
                drQuanHuyen.Enabled = false;
                OpenDialog("#thongbao");
                if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
            }
        }

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
                "scriptKey", sb.ToString(), true);
        }
        #endregion
        #region[ClearTextbox]
        protected void ClearTextBox()
        {
            txtMaDoiTuong.Text = string.Empty;
            txtHoVaTen.Text = string.Empty;
            txtNgaySinh.Text = string.Empty;
            txtDienThoai.Text = string.Empty;
            txtDiDong.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtDiaChi.Text = string.Empty;
            drGioiTinh.SelectedIndex = 0;
            drChucVu.SelectedIndex = 0;
            drDanhXung.SelectedIndex = 0;
            drPhongBan.SelectedIndex = 0;
            drTinh.SelectedIndex = 0;
            drQuanHuyen.SelectedIndex = 0;
        }
        #endregion

        protected void drTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (drTinh.SelectedIndex == 0)
            {
                drQuanHuyen.Enabled = false;
            }
            else
            {
                drQuanHuyen.Enabled = true;
                var clsQuanHuyen = new Districts_BLL();
                var dtQuanHuyen = clsQuanHuyen.BindGirdDistricts_Province(int.Parse(drTinh.SelectedValue));
                if (dtQuanHuyen != null && dtQuanHuyen.Rows.Count > 0)
                {
                    drQuanHuyen.Items.Clear();
                    drQuanHuyen.Items.Add(new ListItem("-Vui lòng chọn quận huyện-", "-1"));
                    drQuanHuyen.DataSource = dtQuanHuyen;
                    drQuanHuyen.DataValueField = "Districts_Id";
                    drQuanHuyen.DataTextField = "Districts_Name";
                    drQuanHuyen.DataBind();
                }
                else
                {
                    drQuanHuyen.Enabled = false;
                    drQuanHuyen.Items.Clear();
                    if (Page.Master != null)
                        ((Label)Page.Master.FindControl("lbThongBao")).Text = "Tỉnh này không tồn tại quận huyện vui lòng chọn tỉnh khác !";
                    OpenDialog("#thongbao");
                    if (Page.Master != null) ((UpdatePanel)Page.Master.FindControl("updatePanelThongBao")).Update();
                    drTinh.SelectedIndex = 0;
                }
            }
        }
    }
}